Python Lists
๐ Let's Get Loopy with Python Lists ๐โ
Welcome to your Python List Adventure โ where boring bullet points turn into belly laughs! Buckle up, we're about to get list-y!
๐ค What Even Is a List?โ
In Python, a list is like a magical backpack ๐งณ that:
- โ Remembers the order you put stuff in (ordered)
- ๐งฎ Keeps everything neat with numbers starting from 0 (indexed)
- ๐ Let's you swap things in and out like a mood ring (mutable)
- ๐ง Can hold a unicorn, a potato, and a disco ball all at once (heterogeneous)
- ๐งฑ Is created using square brackets
[]
(because... geometry!)
1๏ธโฃ Creating a List โ The Birth of Brillianceโ
# Empty backpack
empty = []
# List of super fun subjects
listOfSubjects = ['physics', 'chemistry', "mathematics"]
# List of ID badges
listOfIds = [0, 1, 2, 3, 4]
# A random party of types
miscList = [0, 'one', 2, 'three']
# Nested mayhem: lists within lists
lists = [['A', 'B', 'C'], ['D', 'E', 'F']]
2๏ธโฃ Adding Stuff โ Make Your List a VIP Club ๐ฐโ
charList = []
charList.append("a")
charList.append("b")
print(charList) # ['a', 'b']
# Insert 'c' at index 3... wait, list only has 2 items? Pythonโs chill with that.
charList.insert(3, "c")
print(charList) # ['a', 'b', 'c']
# Try to insert at index 10. No explosions here.
charList.insert(10, "d")
print(charList) # ['a', 'b', 'c', 'd']
3๏ธโฃ Accessing Items โ The Listโs Peek-a-Boo Game ๐โ
numList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numList[0]) # 0
print(numList[1:5]) # [1, 2, 3, 4]
print(numList[:3]) # [0, 1, 2]
print(numList[7:]) # [7, 8, 9]
print(numList[-8:-5]) # [2, 3, 4] (Reverse magic!)
4๏ธโฃ Modify Like a List Surgeon ๐จโโ๏ธโ
charList = ["a", "b", "c"]
charList[2] = "d"
print(charList) # ['a', 'b', 'd']
5๏ธโฃ Iterating โ Say Hello to Every List Item ๐โ
charList = ["a", "b", "c"]
for x in charList:
print(x)
# Output:
# a
# b
# c
๐ P.S. Pythonโs sensitive! Indent properly or face the wrath of indentation errors.
6๏ธโฃ Is it In the List or Not? The Suspense! ๐ฑโ
charList = ["a", "b", "c"]
if "a" in charList:
print("a is present")
if "d" in charList:
print("d is present")
else:
print("d is NOT present")
7๏ธโฃ Size Matters โ How Long is My List? ๐โ
charList = ["a", "b", "c"]
print(len(charList)) # 3
8๏ธโฃ Delete Like a Ninja โ Bye Bye Items! ๐โ
๐งฝ 8.1 remove() โ Yeet by Valueโ
charList = ["a", "b", "c"]
charList.remove("c")
print(charList) # ['a', 'b']
๐ฟ 8.2 pop() โ Pull Out by Index or Lastโ
charList = ["a", "b", "c", "d"]
charList.pop() # 'd' gone
print(charList) # ['a', 'b', 'c']
charList.pop(1) # 'b' gone
print(charList) # ['a', 'c']
๐จ 8.3 clear() โ List Dusting 101โ
charList = ["a", "b", "c", "d"]
charList.clear()
print(charList) # []
๐ฃ 8.4 del โ The List Assassinโ
charList = ["a", "b", "c", "d"]
del charList[0]
print(charList) # ['b', 'c', 'd']
del charList
# print(charList) # ๐ฅ Boom! NameError
9๏ธโฃ Merging Lists โ When Two Become One โค๏ธโ
charList = ["a", "b", "c"]
numList = [1, 2, 3]
joinedList = charList + numList
print(joinedList) # ['a', 'b', 'c', 1, 2, 3]
charList.extend(numList)
print(charList) # ['a', 'b', 'c', 1, 2, 3]
๐ List Methods โ The Fancy Toolbox ๐งโ
๐ง append()โ
charList = ["a", "b", "c"]
charList.append("d")
print(charList) # ['a', 'b', 'c', 'd']
๐งผ clear()โ
charList = ["a", "b", "c"]
charList.clear()
print(charList) # []
๐ช copy()โ
charList = ["a", "b", "c"]
newList = charList.copy()
print(newList) # ['a', 'b', 'c']
๐ข count()โ
charList = ["a", "b", "c"]
print(charList.count('a')) # 1
๐งฉ extend()โ
charList = ["a", "b", "c"]
numList = [1, 2, 3]
charList.extend(numList)
print(charList) # ['a', 'b', 'c', 1, 2, 3]
๐ index()โ
charList = ["a", "b", "c"]
print(charList.index('a')) # 0
๐ง insert()โ
charList = ["a", "b", "c"]
charList.insert(3, 'd')
print(charList) # ['a', 'b', 'c', 'd']
๐ฟ pop()โ
charList = ["a", "b", "c", "d"]
charList.pop()
print(charList) # ['a', 'b', 'c']
charList.pop(1)
print(charList) # ['a', 'c']
๐๏ธ remove()โ
charList = ["a", "b", "c", "d"]
charList.remove('d')
print(charList) # ['a', 'b', 'c']
๐ reverse()โ
charList = ["a", "b", "c", "d"]
charList.reverse()
print(charList) # ['d', 'c', 'b', 'a']
๐งโโ๏ธ sort()โ
charList = ["a", "c", "b", "d"]
charList.sort()
print(charList) # ['a', 'b', 'c', 'd']
๐ Thatโs All, Folks! ๐ฌโ
Keep calm and append()
on. Python lists are flexible, powerful, and fun โ kinda like duct tape, but for data!
Happy Learning & Happy List-ing! ๐๐